package com.tengits.react.amap3d.maps;

import android.content.Context;
import android.graphics.Color;
import com.amap.api.maps.AMap;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.views.view.ReactViewGroup;
import com.tengits.react.amap3d.AMapUtils;

import java.util.ArrayList;

class AMapPolyline extends ReactViewGroup implements AMapOverlay {
  private Polyline polyline = null;
  private ArrayList<LatLng> coordinates = new ArrayList<>();
  private ArrayList<Integer> colors = new ArrayList<>();

  public Float width = 1.0f;
  public Integer color = Color.BLACK;
  public Float zIndex = 1.0f;
  public Boolean geodesic = false;
  public Boolean dashed = false;
  public Boolean gradient = false;

  public AMapPolyline(Context context) {
    super(context);
  }

  public void setWidth(Float value) {
    width = value;
    polyline.setWidth(value);
  }

  public void setColor(Integer value) {
    color = value;
    polyline.setColor(value);
  }

  public void setZIndex(Float value) {
    zIndex = value;
    polyline.setZIndex(value);
  }

  public void setGeodesic(Boolean value) {
    geodesic = value;
    polyline.setGeodesic(value);
  }

  public void setDashed(Boolean value) {
    dashed = value;
    polyline.setDottedLine(value);
  }

  public void setGradient(Boolean value) {
    this.gradient = value;
  }

  public void setCoordinates(ReadableArray coordinates) {
    this.coordinates = AMapUtils.toLatLngList(coordinates);
    polyline.setPoints(this.coordinates);
  }

  public void setColors(ReadableArray cs) {
    this.colors.clear();
    for(int i = 0 ; i < cs.size(); i++) {
      this.colors.add(cs.getInt(i));
    }
  }

  @Override
  public void add(AMap map) {
    polyline = map.addPolyline(new PolylineOptions()
      .addAll(coordinates)
      .color(color)
      .colorValues(colors)
      .width(width)
      .useGradient(gradient)
      .geodesic(geodesic)
      .setDottedLine(dashed)
      .zIndex(zIndex));
  }

  @Override
  public void remove() {
    if(polyline != null) {
      polyline.remove();
    }
    polyline = null;
  }
}
